package com.easylinkin.linkappapi.elasticsearch.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.vo.DeviceQueryVo;
import com.easylinkin.linkappapi.elasticsearch.entity.*;
import com.easylinkin.linkappapi.elasticsearch.service.IEService;
import com.easylinkin.linkappapi.elasticsearch.util.EsCalendar;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("/es")
@Api(value="ES查询",tags={"ES查询"})
public class EsController {

    @Resource
    IEService esService;

    /**
     * <p>
     * 获取设备最新的用电量列表+当天的总用电+最新的总用电量
     * </p>
     */
    @ApiOperation("获取设备最新的用电量列表+当天的总用电+最新的总用电量")
    @PostMapping("/getStatistics")
    public RestMessage getStatistics(@RequestBody EsQuerymodel requestModel) throws Exception {
        String useKinds = requestModel.getUseKinds();
        String dataSourceId = requestModel.getDataSourceId();
        Assert.notNull(requestModel, "customQueryParams 不能为空");
        Assert.notNull(useKinds, "能源类型不能为空");
        Assert.notNull(dataSourceId, "数据源Id不能为空");
        EsHistogramResponseModel esResponsemodel = esService.histogramStatistic(requestModel);
        return RestBuilders.successBuilder("").data(esResponsemodel).build();
    }

    @PostMapping("/queryAiCamera")
    public RestMessage queryAiCamera(@RequestBody RequestModel<AiCameraVo> requestModel) {
        Assert.notNull(requestModel, "customQueryParams 不能为空");
        IPage<AiCameraVo> aiCameraVoIPage = esService.queryAiCamera(requestModel);
        return RestBuilders.successBuilder().data(aiCameraVoIPage).build();
    }

    /**
     * <p>
     * 获取设备最新的用电量列表+当天的总用电+最新的总用电量
     * </p>
     */
    @ApiOperation("获取设备最新的用电量列表+当天的总用电+最新的总用电量")
    @PostMapping("/getRealDevicePage")
    public RestMessage getRealDevicePage(@RequestBody RequestModel<EsQuerymodel> requestModel) throws IOException {
        EsQuerymodel queryModel = requestModel.getCustomQueryParams();
        Page page = requestModel.getPage();
        Assert.notNull(queryModel, "customQueryParams 不能为空");
        Assert.notNull(page, "page 不能为空");
        EsResponsemodel esResponsemodel = new EsResponsemodel();
        queryModel.setQueryTimeStart(EsCalendar.getStartTimeOfDay(1));
        queryModel.setQueryTimeEnd(EsCalendar.getCurerntTime());
        return RestBuilders.successBuilder().data(esService.getRealDevicePage(queryModel, esResponsemodel, page)).build();
    }

    /**
     * 导出
     */
    @ApiOperation("导出能源报表")
    @PostMapping("/exportEnergyReport")
    public void exportEnergyReport(@RequestBody EsQuerymodel queryModel, HttpServletRequest request, HttpServletResponse response) {
        try {
            esService.exportEnergyReport(queryModel, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            response.setHeader("errormessage", e.getMessage());
        }
    }

    /**
     * <p>
     * 环比
     * </p>
     */
    @ApiOperation("环比统计")
    @PostMapping("/momStatistic")
    public RestMessage momStatistic(@RequestBody EsQuerymodel requestModel) throws JsonProcessingException, ParseException {

        MomStatisticModel resultmodel = esService.getMomStatisticData(requestModel);

        return RestBuilders.successBuilder("").data(resultmodel).build();
    }

    /**
     * <p>
     * 柱状图统一数据接口
     * </p>
     */
    @ApiOperation("柱状图统一数据接口")
    @PostMapping("/histogramStatistic")
    public RestMessage histogramStatistic(@RequestBody EsQuerymodel requestModel) throws JsonProcessingException, ParseException {
        EsHistogramResponseModel esResponsemodel = esService.histogramStatistic(requestModel);
        return RestBuilders.successBuilder("").data(esResponsemodel).build();
    }

    /**
     * 卡片 日周月统计 接口
     */
    @ApiOperation("卡片 日周月统计 接口")
    @PostMapping("/dayWeekMonthStatistic")
    public RestMessage dayWeekMonthStatistic(@RequestBody EsQuerymodel esQuerymodel) {
        DayWeekMonthStatisticModel dayWeekMonthStatisticModel = esService.dayWeekMonthStatistic(esQuerymodel);
        return RestBuilders.successBuilder().data(dayWeekMonthStatisticModel).build();
    }

    /**
     * 卡片 自定义时间统计 接口
     */
    @ApiOperation("卡片 自定义时间统计 接口")
    @PostMapping("/customPeriodStatistic")
    public RestMessage customPeriodStatistic(@RequestBody EsQuerymodel esQuerymodel) {
        CustomPeriodStatisticModel customPeriodStatisticModel = esService.customPeriodStatistic2(esQuerymodel);
        return RestBuilders.successBuilder().data(customPeriodStatisticModel).build();
    }

    @ApiOperation("贷后管理报表")
    @PostMapping("exportBankExcel")
    public void exportBankExcel(@RequestBody EsQuerymodel requestModel, HttpServletRequest request, HttpServletResponse response) {
        esService.exportBankExcel(requestModel.getUseKinds(), request, response);
    }

    /**
     * 根据设备code查询能耗统计
     * @param requestModel
     * @return
     */
    @ApiOperation("根据设备code查询能耗统计")
    @PostMapping("energyStatisticsByDeviceCode")
    public RestMessage energyStatisticsByDeviceCode(@RequestBody EsQuerymodel requestModel) {
        return RestBuilders.successBuilder().data(esService.energyStatisticsByDeviceCode(requestModel)).build();
    }

    @ApiOperation("获取能耗分析数据按照空间分组")
    @PostMapping("energyAnalyzeBySpace")
    public RestMessage energyAnalyzeBySpace(@RequestBody EsQuerymodel requestModel) {
        Assert.notNull(requestModel, "requestModel不能为空");
        return RestBuilders.successBuilder().data(esService.energyAnalyzeBySpace(requestModel)).build();
    }

    @ApiOperation("获取区域下设备年、月、日能耗分析")
    @PostMapping("energyAnalyzeByRoleAuthority")
    public RestMessage energyAnalyzeByRoleAuthority(@RequestBody EsQuerymodel requestModel) {
        Assert.notNull(requestModel, "requestModel不能为空");
        Assert.notNull(requestModel.getAreaId(), "areaId 不能为空");
        return RestBuilders.successBuilder().data(esService.energyAnalyzeByRoleAuthority(requestModel)).build();
    }

    /**
     * 根据条件从ES查询数据进行分组
     * 时间周期ES数据-》支持租户、设备、采集类型分组-》聚合类型统计
     * @param requestModel 入参对象
     * @return 出参统一对象
     */
    @ApiOperation("设备采集数据周期内按设备、采集类型、聚合类型分组")
    @PostMapping("/getDeviceCollectDataAggFromEs")
    public RestMessage getDeviceCollectDataAggFromEs(@RequestBody RequestModel<EsDeviceCollectModel> requestModel) {
        Assert.notNull(requestModel, "参数为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "参数不完整");
        return RestBuilders.successBuilder().data(esService.getDeviceCollectDataAggFromEs(requestModel)).build();
    }

    /**
     * 根据条件从ES查询数据进行分组
     * 时间周期ES数据-支持各种es支持的查询参数
     * @param paramMap 入参对象
     * @return 查询结果
     */
    @ApiOperation("设备采集数据周期内按设备、采集类型、聚合类型分组")
    @PostMapping("/getDeviceCollectDataAggFromEs2")
    public RestMessage getDeviceCollectDataAggFromEs2(@RequestBody Map<String,Object> paramMap) {
        Assert.notNull(paramMap, "参数为空");
        return RestBuilders.successBuilder().data(esService.getDeviceCollectDataAggFromEs2(paramMap, EsCalendar.getDeviceFlowIndex(2))).build();
    }


    /**
     * 根据设备，分组统计各设备在查询时间内的能耗
     *
     * @param deviceQueryVo 入参对象
     * @return 统计结果
     */
    @ApiOperation("根据设备，分组统计各设备在查询时间内的能耗")
    @PostMapping("/statisticsEnergyGroupByDevice")
    public RestMessage statisticsEnergyGroupByDevice(@RequestBody DeviceQueryVo deviceQueryVo) {
        Assert.notNull(deviceQueryVo, "参数为空");
        return RestBuilders.successBuilder().data(esService.statisticsEnergyGroupByDevice(deviceQueryVo)).build();
    }

    /**
     * 根据设备，分组统计各设备在查询时间内的能耗,求设备排名前n的
     *
     * @param deviceQueryVo 入参对象
     * @return 统计结果
     */
    @ApiOperation("根据设备，分组统计各设备在查询时间内的能耗,求设备排名前n的")
    @PostMapping("/statisticsEnergyGroupByDeviceTopN")
    public RestMessage statisticsEnergyGroupByDeviceTopN(@RequestBody DeviceQueryVo deviceQueryVo) {
        Assert.notNull(deviceQueryVo, "参数为空");
        return RestBuilders.successBuilder().data(esService.statisticsEnergyGroupByDeviceTopN(deviceQueryVo)).build();
    }


    /**
     * 根据设备，分组统计各设备在查询时间内的能耗,根据时间各设备的折线图
     *
     * @param deviceQueryVo 入参对象
     * @return 统计结果
     */
    @ApiOperation("根据设备，分组统计各设备在查询时间内的能耗,根据时间各设备的折线图")
    @PostMapping("/statisticsEnergyGroupByDeviceByTime")
    public RestMessage statisticsEnergyGroupByDeviceByTime(@RequestBody DeviceQueryVo deviceQueryVo) {
        Assert.notNull(deviceQueryVo, "参数为空");
        return RestBuilders.successBuilder().data(esService.statisticsEnergyGroupByDeviceByTime2(deviceQueryVo)).build();
    }

    /**
     * 根据设备，分组统计各设备在查询时间内的能耗,根据时间各设备的折线图，定时采集
     *
     * @param deviceQueryVo 入参对象
     * @return 统计结果
     */
    @ApiOperation("根据设备，分组统计各设备在查询时间内的能耗,根据时间各设备的折线图")
    @PostMapping("/collectStatisticsEnergyGroupByDeviceByTime")
    public RestMessage collectStatisticsEnergyGroupByDeviceByTime(@RequestBody DeviceQueryVo deviceQueryVo) {
        Assert.notNull(deviceQueryVo, "参数为空");
        return RestBuilders.successBuilder().data(esService.collectStatisticsEnergyGroupByDeviceByTime(deviceQueryVo)).build();
    }


}
